package edu.utd.text.game.designer.commands;

import java.util.Scanner;

import edu.utd.text.game.controller.commands.MovementCommand;
import edu.utd.text.game.controller.parsing.CommandParser;
import edu.utd.text.game.designer.controller.DesignerController;
import edu.utd.text.game.domain.areas.Direction;
import edu.utd.text.game.domain.areas.Directions;
import edu.utd.text.game.domain.areas.Room;

public class ConnectRoomsCommand implements DesignerCommand {


	public String execute() throws Exception {
		Scanner scanner = new Scanner(System.in);
		System.out.println("Source room: ");
		String srcRoomName = scanner.nextLine();
		Room srcRoom = DesignerController.INSTANCE.getGameMap().getRoomByName(srcRoomName);
		if (null == srcRoom) {
			return "Cannot find room: \"" + srcRoomName + "\" in game map.";
		}
		System.out.println("Source room exit direction: ");
		String srcRoomExit = scanner.nextLine();
		MovementCommand mc = CommandParser.INSTANCE.parseInputForMovement(srcRoomExit);
		Direction srcRoomExitDirection = mc.getDirectionToMove();
		Direction destRoomExitDirection = Directions.getOppositeDirection(srcRoomExitDirection);
		System.out.println("Destination room: ");
		String destRoomName = scanner.nextLine();
		Room destRoom = DesignerController.INSTANCE.getGameMap().getRoomByName(destRoomName);
		if (null == destRoom) {
			return "Cannot find room: \"" + destRoomName + "\" in game map.";
		}
		System.out.println("Destination room direction (default: " 
				+ destRoomExitDirection + ")");
		String destRoomExit = scanner.nextLine();
		if (!destRoomExit.isEmpty() && destRoomExit != "") {
			mc = CommandParser.INSTANCE.parseInputForMovement(destRoomExit);
			destRoomExitDirection = mc.getDirectionToMove();
		}
		setExit(srcRoom, destRoom, srcRoomExitDirection);
		setExit(destRoom, srcRoom, destRoomExitDirection);
		return "Successfully connected room " + 
			srcRoom.getRoomName() + " (" + srcRoomExitDirection + " exit) to " + 
			destRoom.getRoomName() + " (" + destRoomExitDirection + " exit).";
	}
	
	public void setExit(Room from, Room to, Direction direction) {
		switch (direction) {
		case NORTH		:	from.setNorthExit(to); 		break;
		case SOUTH		:	from.setSouthExit(to); 		break;
		case EAST		: 	from.setEastExit(to); 		break;
		case WEST		: 	from.setWestExit(to);		break;
		case NORTHEAST	:	from.setNorthEastExit(to);	break;
		case NORTHWEST	:	from.setNorthWestExit(to);	break;
		case SOUTHEAST	: 	from.setSouthEastExit(to);	break;
		case SOUTHWEST	:	from.setSouthWestExit(to);	break;
		}
	}

}
